﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using 个人博客.SqlViews;

namespace 个人博客.instrumentClass
{
    public class GmailClass
    {
        //发送邮件
        public void sendGmail(string Gmail,string YZM) {
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.From = new MailAddress("2448546634@qq.com");
            mail.To.Add(new MailAddress(Gmail));
            mail.Subject = "来自遥远的呼唤";
            mail.Body = "您的验证码是 : " + YZM;
            SmtpClient client = new SmtpClient("smtp.qq.com");
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential("2448546634@qq.com", "jiiansbldvfjebdh");
            client.Send(mail);
        }
        //Gmail验证码
        public string GmailVerificationCode() {
            string[] str = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            Random R = new Random();
            string verificationCode = "";
            for (int i = 0; i < 6; i++)
            {
                if (R.Next(1, 3) == 1)
                {
                    verificationCode = verificationCode + str[R.Next(0, 26)];
                }
                else 
                {
                    verificationCode = verificationCode + R.Next(0, 10).ToString();
                }
            }
            return verificationCode;
        }
        //是否已经拥有该邮箱
        public bool YouEmail(string Email) {
            using (var db = new Model1())
            {
                var list = db.Blogs_Account.FirstOrDefault(u => u.acc_gmail == Email);
                if (list == null) {
                    return false;
                }
                return true;
            }
        }
        //验证邮箱
        public bool CheckEmail(string Email)
        {
            bool Flag = false;
            string str = "([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,5})+";
            string[] result = GetPathPoint(Email, str);
            if (result != null)
            {
                Flag = result.Contains(Email) ? true : Flag;
            }
            return Flag;
        }
        public string[] GetPathPoint(string value, string regx)
        {
            if (string.IsNullOrWhiteSpace(value))
                return null;
            bool isMatch = System.Text.RegularExpressions.Regex.IsMatch(value, regx);
            if (!isMatch)
                return null;
            System.Text.RegularExpressions.MatchCollection matchCol = System.Text.RegularExpressions.Regex.Matches(value, regx);
            string[] result = new string[matchCol.Count];
            if (matchCol.Count > 0)
            {
                for (int i = 0; i < matchCol.Count; i++)
                {
                    result[i] = matchCol[i].Value;
                }
            }
            return result;
        }

    }
}